{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-03-03T04:01:25.066565Z",
     "start_time": "2025-03-03T04:01:23.140118Z"
    }
   },
   "source": [
    "import pandas as pd\n",
    "import numpy as np"
   ],
   "outputs": [],
   "execution_count": 1
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T04:02:46.498513Z",
     "start_time": "2025-03-03T04:02:46.494483Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df = pd.DataFrame(np.random.randn(5,4)-1)\n",
    "print(df)\n",
    "print('-'*50)\n",
    "print(abs(df))  # 取绝对值"
   ],
   "id": "3f8dddc935fb2f32",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "          0         1         2         3\n",
      "0 -0.075625 -0.898156 -1.491895 -1.279390\n",
      "1 -1.628385  0.223405 -1.456127  0.101044\n",
      "2 -1.595388 -2.147482 -0.772864 -0.900466\n",
      "3 -1.395511 -0.487724 -1.697870 -3.022386\n",
      "4 -0.533560 -1.025155 -0.356638 -0.348057\n",
      "--------------------------------------------------\n",
      "          0         1         2         3\n",
      "0  0.075625  0.898156  1.491895  1.279390\n",
      "1  1.628385  0.223405  1.456127  0.101044\n",
      "2  1.595388  2.147482  0.772864  0.900466\n",
      "3  1.395511  0.487724  1.697870  3.022386\n",
      "4  0.533560  1.025155  0.356638  0.348057\n"
     ]
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### apply()函数",
   "id": "c4c6b6b2d7c602a6"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T04:05:19.208798Z",
     "start_time": "2025-03-03T04:05:19.204492Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# apply()函数可以对数据框中的每一行或每一列进行操作.默认情况下,apply()函数会对每一列进行操作.因为axis参数默认为0.\n",
    "print(df.apply(lambda x : max(x),axis = 0))"
   ],
   "id": "db6b0f0a85afcf33",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0   -0.075625\n",
      "1    0.223405\n",
      "2   -0.356638\n",
      "3    0.101044\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T04:06:54.971058Z",
     "start_time": "2025-03-03T04:06:54.967829Z"
    }
   },
   "cell_type": "code",
   "source": "print(df.apply(lambda x : max(x),axis = 1))  # 对每一行进行操作",
   "id": "3868035e736ba41",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0   -0.075625\n",
      "1    0.223405\n",
      "2   -0.772864\n",
      "3   -0.487724\n",
      "4   -0.348057\n",
      "dtype: float64\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "### map()函数  ",
   "id": "177303c81560e6f3"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-03-03T04:11:07.672064Z",
     "start_time": "2025-03-03T04:11:07.666084Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(df.map(lambda x : '%.2f'% x))  #amp()函数可以作用到每一个元素上\n",
    "df.dtypes  # 查看数据类型"
   ],
   "id": "460769152a386c4b",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       0      1      2      3\n",
      "0  -0.08  -0.90  -1.49  -1.28\n",
      "1  -1.63   0.22  -1.46   0.10\n",
      "2  -1.60  -2.15  -0.77  -0.90\n",
      "3  -1.40  -0.49  -1.70  -3.02\n",
      "4  -0.53  -1.03  -0.36  -0.35\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "0    float64\n",
       "1    float64\n",
       "2    float64\n",
       "3    float64\n",
       "dtype: object"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
